package cn.edu.scnu.controller;

import cn.edu.scnu.entity.*;
import cn.edu.scnu.service.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Flow;

@Controller
public class OrderController {
    private CustomerService customerService;
    private CartService cartService;
    private OrderSevice orderService;

    private ShoworderService showorderService;
    private FlowerService flowerService;




    public  OrderController(CustomerService customerService, CartService cartService, OrderSevice orderSevice, ShoworderService showorderService, FlowerService flowerService){
        this.cartService = cartService;
        this.orderService = orderSevice;
        this.customerService =customerService;
        this.showorderService = showorderService;
        this.flowerService = flowerService;
    }
    @RequestMapping("/order/order")
    public String order(String cartIds, HttpSession session, Model model){
        TbMember member = (TbMember)session.getAttribute("memberLogin");
        if(member == null)
            return "redirect:/index/toLogin";
        model.addAttribute("member", member);
        List<Customer> customers = customerService.findByEmail(member.getEmail());
        model.addAttribute("customers", customers);
        String[] arrayCartIds = cartIds.split(",");
        List<Cart> carts = new ArrayList<>();
        int sum = 0, total = 0;
        for(String cid : arrayCartIds){
            Integer cartId = Integer.valueOf(cid);
            Cart cart = cartService.getById(cartId);
            carts.add(cart);
            sum += cart.getNum();
            total += cart.getNum() * cart.getYourprice();
        }
        model.addAttribute("sum", sum);
        model.addAttribute("total", total);
        model.addAttribute("carts", carts);
        model.addAttribute("cartIds", cartIds);
        return "order";
    }

    @RequestMapping("/order/addOrder")
    @ResponseBody
    public String addOrder(Myorder myorder, String cartIds, HttpSession session) {
        TbMember member = (TbMember) session.getAttribute("memberLogin");
        if (member == null)
            return "redirect:/index/toLogin";
        myorder.setOrderId(UUID.randomUUID().toString());
        myorder.setEmail(member.getEmail());
        String[] arrayCartIds = cartIds.split(",");
        Date date = new Date();
        myorder.setInputtime(new Timestamp(date.getTime()));
        for (String cid : arrayCartIds) {
            Integer cartId = Integer.valueOf(cid);
            Cart cart = cartService.getById(cartId);
            myorder.setFlowerid(cart.getFlowerid());
            myorder.setNum(cart.getNum());
            orderService.save(myorder);
            myorder.setId(null);
        }
        return "success";
    }
    @RequestMapping("/order/showorder")
    public String showorder(String status, HttpSession session, Model model) {
        TbMember member = (TbMember) session.getAttribute("memberLogin");
        if (member == null)
            return "redirect:/index/toLogin";
        List<Showorder> showorders = showorderService.showorder(member.getEmail(), status);
        model.addAttribute("showorders", showorders);
        return "showorder";
    }

    @RequestMapping("/order/delorder")
    public String delorder(Integer id){
        orderService.removeById(id);
        return "forward:/order/showorder";
    }

    @RequestMapping("/order/pay")
    public String pay(Integer id){
        Myorder myorder = orderService.getById(id);
        myorder.setStatus("已付款");
        orderService.updateById(myorder);
        return "forward:/order/showorder";
    }
    @RequestMapping("/order/updateOrder")
    public String updateOrder(Integer id) {
        Myorder myorder = orderService.getById(id);
        Flower flower = flowerService.getById(myorder.getFlowerid());
        flower.setSellednum(flower.getSellednum() + myorder.getNum());
        flowerService.updateFlowerById(flower);
        myorder.setStatus("未评价");
        orderService.updateById(myorder);
        return "forward:/order/showorder";
    }
    @RequestMapping("/order/evaluate")
    public String evaluate(Integer id,Model model){
        Showorder showorder = showorderService.getById(id);
        model.addAttribute("showorder",showorder);
        return "evaluate";
    }
    @RequestMapping("/order/doEvaluate")
    public String doEvaluate(HttpServletRequest request) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Myorder myorder = orderService.getById(id);
        myorder.setPjip(request.getHeader("HTTP_CLIENT_IP"));
        myorder.setPjcontent(request.getParameter("pjcontent" + id));
        int pjstar = Integer.parseInt(request.getParameter("pjstar" + id));
        myorder.setPjstar(pjstar);
        myorder.setPjtime(new Timestamp(System.currentTimeMillis()));
        myorder.setStatus("已评价");
        orderService.updateById(myorder);
        return "forward:/order/showorder";
    }
}
